// pages/head/head.js
const db=wx.cloud.database();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    objectType:["全部","生活用品","贵重物品","学习用品","衣服","电子物品","餐饮用品"],
    Type:['全部','失物','拾物'],
    objectList:[],
    select1:0,
    select2:0
  },
  click(){
    wx.navigateTo({
      url: '/pages/add/add'
    })
  },
  select1(e){
    this.setData({
      select1: e.currentTarget.dataset.index
    })
    this.onLoad()
  },
  select2(e){
    this.setData({
      select2: e.currentTarget.dataset.index
    })
    this.onLoad()
  },
  onclick(e){
    const index=e.currentTarget.dataset.index;
    wx.navigateTo({
      url: '/pages/show/show?_id='+this.data.objectList[index]._id
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if(this.data.select1==0&&this.data.select2==0) {
      db.collection("object").get({
        success:res=>{
          let list=res.data;
         list.sort((a, b) => {
            const timeA = new Date(a.createtime).getTime(); // 转换为时间戳
            const timeB = new Date(b.createtime).getTime();
            return timeB - timeA; // 降序
          });
          this.setData({
            objectList:list
          })
          }
        })
    }
    else if (this.data.select1==0&&this.data.select2!=0) {
      db.collection("object").where({
        Type:this.data.Type[this.data.select2]
      }).get({
        success:res=>{
          let list=res.data;
         list.sort((a, b) => {
            const timeA = new Date(a.createtime).getTime(); // 转换为时间戳
            const timeB = new Date(b.createtime).getTime();
            return timeB - timeA; // 降序
          });
          this.setData({
            objectList:list
          })
          }
        })
    }
    else if (this.data.select1!=0&&this.data.select2==0) {
      db.collection("object").where({
        objectType:this.data.objectType[this.data.select1],
      }).get({
        success:res=>{
          let list=res.data;
        list.sort((a, b) => {
            const timeA = new Date(a.createtime).getTime(); // 转换为时间戳
            const timeB = new Date(b.createtime).getTime();
            return timeB - timeA; // 降序
          });
          this.setData({
            objectList:list
          })
          }
        })
    }
    else{
    db.collection("object").where({
      objectType:this.data.objectType[this.data.select1],
      Type:this.data.Type[this.data.select2]
    }).get({
      success:res=>{
        let list=res.data;
        list.sort((a, b) => {
          const timeA = new Date(a.createtime).getTime(); // 转换为时间戳
          const timeB = new Date(b.createtime).getTime();
          return timeB - timeA; // 降序
        });
        this.setData({
          objectList:list
        })
        }
      })
  }
},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.onLoad();
    console.log("数据获取成功！");
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh:function() {
    console.log("触发了下拉刷新"),
    this.onLoad(),
    wx.stopPullDownRefresh()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})